//
//  ProductDetailVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/3.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "ProductDetailVC.h"
#import "SDCycleScrollView.h"
#import "HandInPaymentVC.h"
#import "HandInPaymentSuccessVC.h"
#import <WebKit/WebKit.h>
#import "BAKit_WebView.h"

#pragma mark -- mark 数据模型
#import "ProductContentDetailModel.h"

@interface ProductDetailVC ()<SDCycleScrollViewDelegate, WKUIDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;

/**
 滚动banner
 */
@property (nonatomic, strong) SDCycleScrollView *bannerView;

/**
 title
 */
@property (nonatomic, strong) UILabel *titleLabel;

/**
 价格
 */
@property (nonatomic, strong) UILabel *priceLabel;

/**
 数量
 */
@property (nonatomic, strong) UILabel *countLabel;

/**
 规格
 */
@property (nonatomic, strong) UILabel *specLabel;

/// 买方
@property (nonatomic, strong) UILabel *salerLabel;

/**
 市场参考价
 */
@property (nonatomic, strong) UILabel *advicePriceLabel;

/**
 下图片（可以加载完数据之后在添加）
 */
@property (nonatomic, strong) UIImageView *bottomImageView;

/**
 展示html文件
 */
@property (nonatomic, strong) WKWebView *webView;

/// 分享按钮view
@property (nonatomic, strong) UIView *shareButtonView;
@end

@implementation ProductDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = !self.type ? @"商品详情" : @"积分兑换";
    self.view.backgroundColor = SKColorWithHex(0xF1F1F1);
    [self initContentViews];
    [self requestContentData];
}

- (void)initContentViews {
    CGFloat margin = 15.0;
    
    UIButton *butNowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (self.type == 0) {
        // 购买
        [butNowBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    }else if (self.type == 1) {
        // 积分兑换
        [butNowBtn setTitle:@"我要兑换" forState:UIControlStateNormal];
        [butNowBtn setTitle:@"积分不足" forState:UIControlStateDisabled];
    }
    [butNowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    butNowBtn.titleLabel.font = SKFONT_BOLD_SIZE(19);
    butNowBtn.backgroundColor = SKColorWithHex(0xE33030);
    [butNowBtn addTarget:self action:@selector(buyNowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butNowBtn];
    [butNowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(-SKBottomSafe_Height);
        make.height.mas_equalTo(butNowBtn.mas_width).multipliedBy(49.0/375);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.width.mas_equalTo(self.view.mas_width);
        make.top.mas_offset(SKTopBar_Height);
        if (self.showType == ProductDetailTypeLog) {
            make.bottom.mas_offset(-SKBottomSafe_Height);
        }else {
            make.bottom.mas_equalTo(butNowBtn.mas_top).mas_offset(0);
        }
    }];
    
    [self.scrollView addSubview:self.bannerView];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.width.mas_equalTo(self.view.mas_width);
        make.height.mas_equalTo(self.bannerView.mas_width).multipliedBy(200 / 375);
    }];
    
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bannerView);
        make.top.mas_equalTo(self.bannerView.mas_bottom);
        make.height.mas_equalTo(view1.mas_width).multipliedBy(77.5 / 375);
    }];
    
    UILabel *tipLabel1 = [[UILabel alloc] init];
    tipLabel1.text = !self.type ? @"现价：" : @"积分：";
    tipLabel1.textColor = self.priceLabel.textColor;
    tipLabel1.font = SKFONT_SIZE(13);
    [view1 addSubview:tipLabel1];
    [view1 addSubview:self.titleLabel];
    [view1 addSubview:self.priceLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(margin);
        make.right.mas_offset(-margin);
        make.bottom.mas_equalTo(view1.mas_centerY).mas_offset(-10);
    }];
    
    [tipLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(view1.mas_centerY).mas_offset(12);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(tipLabel1.mas_right).mas_offset(4);
        make.bottom.mas_equalTo(tipLabel1.mas_bottom).mas_offset(2);
        make.right.mas_lessThanOrEqualTo(-margin);
    }];
    
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bannerView);
        make.top.mas_equalTo(view1.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(view2.mas_width).multipliedBy(45.0/375);
    }];
    
    UILabel *tipLabel2 = [[UILabel alloc] init];
    tipLabel2.font = SKFONT_SIZE(14);
    tipLabel2.textColor = SKColorWithHex(0x999999);
    tipLabel2.text = @"数量";
    [view2 addSubview:tipLabel2];
    [view2 addSubview:self.countLabel];
    [tipLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_offset(0);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-margin);
        make.centerY.mas_offset(0);
    }];
    
    UIView *view3 = [[UIView alloc] init];
    view3.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view3];
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view2);
        make.top.mas_equalTo(view2.mas_bottom).mas_offset(0.5);
    }];
    UILabel *tipLabel3 = [[UILabel alloc] init];
    tipLabel3.font = tipLabel2.font;
    tipLabel3.textColor = tipLabel2.textColor;
    tipLabel3.text = @"规格";
    [view3 addSubview:tipLabel3];
    [view3 addSubview:self.specLabel];
    [tipLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_offset(0);
    }];
    [self.specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.countLabel);
        make.centerY.mas_offset(0);
    }];
    
    UIView *salerView = [[UIView alloc] init];
    salerView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:salerView];
    [salerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view2);
        make.top.mas_equalTo(view3.mas_bottom).mas_offset(0.5);
    }];
    UILabel *salerTipLabel = [[UILabel alloc] init];
    salerTipLabel.font = tipLabel2.font;
    salerTipLabel.textColor = tipLabel2.textColor;
    salerTipLabel.text = @"供货方";
    [salerView addSubview:salerTipLabel];
    [salerView addSubview:self.salerLabel];
    [salerTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_offset(0);
    }];
    [self.salerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.countLabel);
        make.centerY.mas_offset(0);
    }];
    
    UIView *view4 = [[UIView alloc] init];
    view4.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view4];
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(view2);
        make.top.mas_equalTo(salerView.mas_bottom).mas_offset(0.5);
    }];
    UILabel *tipLabel4 = [[UILabel alloc] init];
    tipLabel4.textColor = tipLabel2.textColor;
    tipLabel4.font = tipLabel2.font;
    tipLabel4.text = @"市场参考价";
    [view4 addSubview:tipLabel4];
    [view4 addSubview:self.advicePriceLabel];
    [tipLabel4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.centerY.mas_offset(0);
    }];
    [self.advicePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.countLabel);
        make.centerY.mas_offset(0);
    }];
    
    
//    [self.scrollView addSubview:self.bottomImageView];
//    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(view2);
//        make.top.mas_equalTo(view4.mas_bottom).mas_offset(0);
//    }];

    [self.scrollView addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(view2);
        make.top.mas_equalTo(view4.mas_bottom).mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    
    [self.shareButtonView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(butNowBtn.mas_top).mas_offset(-10);
    }];
    [self.view bringSubviewToFront:self.shareButtonView];
    if (self.type == ProductDetailTypeLog) {
        self.shareButtonView.hidden = true;
    }
}

#pragma mark -- mark 懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (SDCycleScrollView *)bannerView {
    if (!_bannerView) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:SKIMAGE_NAMED(@"bannerPlacehoder")];
        _bannerView.currentPageDotImage = SKIMAGE_NAMED(@"shop_banner_pagecontrol_current_img");
        _bannerView.pageDotImage = SKIMAGE_NAMED(@"shop_banner_pagecontrol_default_img");
        _bannerView.infiniteLoop = true;
    }
    return _bannerView;
}
- (UIView *)shareButtonView {
    if (!_shareButtonView) {
        _shareButtonView = [[UIView alloc] initWithFrame:CGRectZero];
        _shareButtonView.layer.masksToBounds = true;
        _shareButtonView.layer.borderWidth = 0.5;
        _shareButtonView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _shareButtonView.layer.shadowColor = [UIColor cyanColor].CGColor;
        _shareButtonView.layer.shadowOpacity = 0.5;
        _shareButtonView.layer.shadowOffset = CGSizeMake(20, 20);
        _shareButtonView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8];
        _shareButtonView.layer.cornerRadius = 20.0;
        [self.view addSubview:_shareButtonView];
        [_shareButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareButton setImage:SKIMAGE_NAMED(@"shop_share_btn_img_1") forState:UIControlStateNormal];
        [shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_shareButtonView addSubview:shareButton];
        [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(0);
        }];
    }
    return _shareButtonView;
}
- (WKWebView *)webView {
    if (!_webView)
    {
        _webView = [WKWebView new];
        _webView.ba_web_isAutoHeight = YES;
        //  添加 WKWebView 的代理，注意：用此方法添加代理
        BAKit_WeakSelf
        [_webView ba_web_initWithDelegate:weak_self.webView uIDelegate:weak_self.webView];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.userInteractionEnabled = true;
        _webView.ba_web_getCurrentHeightBlock = ^(CGFloat currentHeight) {
            BAKit_StrongSelf
            [self.webView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(currentHeight);
            }];
            [self.scrollView layoutIfNeeded];
        };
    }
    return _webView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = SKFONT_BOLD_SIZE(18);
        _titleLabel.textColor = SKColorWithHex(0x333333);
        _titleLabel.text = @"商品名称";
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = SKFONT_Medium(16);
        _priceLabel.textColor = SKColorWithHex(0xFF3030);
        _priceLabel.text = !self.type ? @"120元" : @"120";
    }
    return _priceLabel;
}

- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [[UILabel alloc] init];
        _countLabel.text = @"一套";
        _countLabel.textColor = SKColorWithHex(0x999999);
        _countLabel.font = SKFONT_SIZE(14);
    }
    return _countLabel;
}
- (UILabel *)specLabel {
    if (!_specLabel) {
        _specLabel = [[UILabel alloc] init];
        _specLabel.font = SKFONT_SIZE(14);
        _specLabel.textColor = SKColorWithHex(0x999999);
        _specLabel.text = @"4杯（6CM-CM）+1壶（12-CM8CM）";
    }
    return _specLabel;
}

- (UILabel *)salerLabel {
    if (!_salerLabel) {
        _salerLabel = [[UILabel alloc] init];
        _salerLabel.font = SKFONT_SIZE(14);
        _salerLabel.textColor = SKColorWithHex(0x999999);
        _salerLabel.text = @" ";
    }
    return _salerLabel;
}
- (UILabel *)advicePriceLabel {
    if (!_advicePriceLabel) {
        _advicePriceLabel = [[UILabel alloc] init];
        _advicePriceLabel.font = SKFONT_SIZE(14);
        _advicePriceLabel.textColor = SKColorWithHex(0x999999);
        _advicePriceLabel.text = @"¥ 165";
    }
    return _advicePriceLabel;
}

- (UIImageView *)bottomImageView {
    if (!_bottomImageView) {
        _bottomImageView = [[UIImageView alloc] init];
        _bottomImageView.image = SKIMAGE_NAMED(@"bannerPlacehoder");
    }
    return _bottomImageView;
}
#pragma mark -- mark SDCycleScrollViewDelegate

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    
}

#pragma mark -- mark WKUIDelegate


#pragma mark -- mark 获取网络数据

- (void)requestContentData {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Shop/getGoodsInfo" params:@{@"id":self.ptId} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        ProductContentDetailModel *model = [ProductContentDetailModel modelWithDictionary:response];
        if ([model isSuccess]) {
            [weakSelf reloadContentDataWith:model];
        }else {
            [weakSelf showMessage:model.msg];
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

#pragma mark -- mark 刷新数据 业务逻辑处理

/// 刷新界面数据
/// @param model model
- (void)reloadContentDataWith:(ProductContentDetailModel *)model {
    NSMutableArray *imgUrls = [NSMutableArray arrayWithCapacity:model.data.lunbo.count];
    for (ProductContentDetailDataBannerModel *bannar in model.data.lunbo) {
        [imgUrls addObject:bannar.imgurl];
    }
    self.bannerView.imageURLStringsGroup = imgUrls;
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrls.firstObject]];
    UIImage *image = [UIImage imageWithData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.bannerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.bannerView.mas_width).multipliedBy(image.size.height / image.size.width);
        }];
    });
    ProductContentDetailDataGoodsModel *goodsInfo = model.data.goods;
    self.titleLabel.text = goodsInfo.name;
    self.priceLabel.text = !self.type ? [NSString stringWithFormat:@"%@元", goodsInfo.price] : goodsInfo.jifen;
    self.countLabel.text = goodsInfo.category;
    self.specLabel.text = goodsInfo.spec;
    self.salerLabel.text = goodsInfo.supplier;
    self.advicePriceLabel.text = [NSString stringWithFormat:@"¥ %@", goodsInfo.refer_price];
    [self.bottomImageView sd_setImageWithURL:[NSURL URLWithString:goodsInfo.img] placeholderImage:SKIMAGE_NAMED(@"detail_default_img")];
    NSString *html = goodsInfo.contents;
    [self.webView loadHTMLString:html baseURL:[NSURL new]];
    
}

#pragma mark -- mark EVENT

- (void)shareButtonClick:(UIButton *)sender {
    NSString *reg_num = [UserInfoSingleton shareInstance].userInfo.reg_num;
    reg_num = (reg_num.length > 0) ? reg_num : @"aaaaaa";
    [AppDelegate wj_wxShareWith:reg_num PtNum:self.ptId SellOrGoodId:false Complete:^(BOOL status) {
        
    }];
}

/**
 立即购买

 @param sender sender
 */
- (void)buyNowButtonClick:(UIButton *)sender {
    // 购买
    if ([[UserInfoSingleton shareInstance].userInfo isLogin]) {
        HandInPaymentVC * vc = [[HandInPaymentVC alloc]init];
        vc.type = (self.type == 1) ? PayTypeScore : PayTypeProduct;
        vc.idStr = self.ptId;
        [self.navigationController pushViewController:vc animated:true];
    }else {
        [AppDelegate pushLoginView];
    }
}
@end
